//index.js
import api from '../../tool/requestModel.js'
let  app = getApp()
Page({
	data: {
		// 当前在第几道问题
		currentIndex: 0,
		goodList: [
			1, 2, 3, 4, 5
		],
		guangGao: false,
		// 选择题回答道第几道了
		xuanZeIndex: 0,
		// 答案提示的文字
		daAnText: [],
		isShowZheng: 1,
		score: 0,
		// 是否是有错误答案的
		zhengQueBtn: false,
		// 广告位置
		guangGaoWei: {},
		ios: false,
		product_id: '',
		getAd: true,
        image:'../../static/deng_fff.png',
		image2:'../../static/deng_fff.png',
		image3:'../../static/deng_fff.png',
		image4:'../../static/deng_fff.png',
        width:"20%",
		donghuaList:[
			"1","2",
			"animate__animated animate__flipInY",   //旋转
		],
        height:""
	},
	onLoad(openit) {
        app.globalData.show = true;
        let height = tt.getSystemInfoSync().screenHeight
        this.setData({
            height:height+'px'
        })
		if (tt.getStorageSync('ios')) {
			this.setData({
				ios: tt.getStorageSync('ios')
			})
		}
		this.setData({
			product_id: openit.product_id
		})
		console.log(openit)
		let box = {}
		if (openit.current) {
			box = {
				topic_property_id: openit.current
			}
		}
		api.tmxqGoodList({
			product_id: openit.product_id,
			...box
		}).then(res => {
			// this.addStyle(data) 
			this.setData({
				goodList: this.addStyle(res.data)
			})
			if (res.data[0].concept_cont) { 
				this.setData({
					zhengQueBtn: true
				})
			}

		})
		tt.setNavigationBarColor({ 
			// 字体颜色 仅支持 #ffffff 和 #000000
			frontColor: '#000000',
			//     背景颜色值，有效值为十六进制颜色
			backgroundColor: '#ffffff',
		});
	},
	addStyle: function(data) {
		data.forEach((item, key) => {
			item.text = ''
			item.xuan_xiang_lists.forEach((iitem, kkey) => {
				iitem.style = "";
			})
		});
		return data;
	},
	// 禁止滑动
	stopMove: function() {
		return true 
	},
	btnClickD: function(item) {
		let index = item.detail
		// console.log(this.currentIndex, index);
		// 有属性时 有答案和错误选项
		// if (this.data.zhengQueBtn) {
		// 	let good = this.data.goodList
		// 	if (this.data.goodList[this.data.currentIndex].xuan_xiang_lists[index].score == 0) {

		// 		good[this.data.currentIndex].xuan_xiang_lists[index].style = "cuoWu";
		// 		good[this.data.currentIndex].text = '回答错误，'
		// 		// 每道题的分数 累计
		// 		this.setData({
		// 			score: this.data.score + good[this.data.currentIndex].xuan_xiang_lists[index].score,
		// 			goodList: good
		// 		})
		// 	} else {
		// 		good[this.data.currentIndex].text = '回答正确，'
		// 		// 每道题的分数 累计
		// 		this.setData({
		// 			score: this.data.score + good[this.data.currentIndex].xuan_xiang_lists[index].score,
		// 			goodList: good
		// 		})
		// 	}
		// 	good[this.data.currentIndex].xuan_xiang_lists.forEach((i, k) => {
		// 		if (i.score !== 0) i.style = "zhengQue";
		// 	})
		// 	this.setData({
		// 		goodList: good
		// 	})
		// } else {
			// 无属性是只显示选择的选项
			let good = this.data.goodList
			good[this.data.currentIndex].xuan_xiang_lists[index].style = 'zhengQue'
			// 每道题的分数 累计
			this.setData({
				score: this.data.score + good[this.data.currentIndex].xuan_xiang_lists[index].score,
				goodList: good
			})
            if(this.data.currentIndex + 1 == this.data.goodList.length){
				this.setData({
					guangGao: true
				})
				// 显示广告时 顶部栏背景
				tt.setNavigationBarColor({
					// 字体颜色 仅支持 #ffffff 和 #000000
					frontColor: '#ffffff',
					backgroundColor: '#101742',
				});
			}else{
                let good = this.data.goodList
				good[this.data.currentIndex].xuan_xiang_lists[index].style="zhengQue"
				this.setData({
					goodList: good
				})
				let that = this
				var donghuaIndex = Math.floor((Math.random()*this.data.donghuaList.length));
                setTimeout(function(){
					if(that.data.donghuaList[donghuaIndex]=='1'){
						good[that.data.currentIndex+1].xuan_xiang_lists[0].style="animate__animated animate__slideInLeft"
						good[that.data.currentIndex+1].xuan_xiang_lists[1].style="animate__animated animate__slideInRight"
						if(good[that.data.currentIndex+1].xuan_xiang_lists[2]){
							good[that.data.currentIndex+1].xuan_xiang_lists[2].style="animate__animated animate__slideInLeft"
						}
						if(good[that.data.currentIndex+1].xuan_xiang_lists[3]){
							good[that.data.currentIndex+1].xuan_xiang_lists[3].style="animate__animated animate__slideInRight"
						}
					}else if(that.data.donghuaList[donghuaIndex]=='2'){
						good[that.data.currentIndex+1].xuan_xiang_lists[0].style="animate__animated animate__slideInRight"
						good[that.data.currentIndex+1].xuan_xiang_lists[1].style="animate__animated animate__slideInLeft"
						if(good[that.data.currentIndex+1].xuan_xiang_lists[2]){
							good[that.data.currentIndex+1].xuan_xiang_lists[2].style="animate__animated animate__slideInRight"
						}
						if(good[that.data.currentIndex+1].xuan_xiang_lists[3]){
							good[that.data.currentIndex+1].xuan_xiang_lists[3].style="animate__animated animate__slideInLeft"
						}
						
					}else{
						good[that.data.currentIndex+1].style=that.data.donghuaList[donghuaIndex]
						
					}
					that.setData({
						goodList: good
					})
					that.setData({
						currentIndex: that.data.currentIndex + 1
					})
					if(that.data.currentIndex==1){
						that.setData({
							image:'../../static/deng_yel.png'
						})
						that.setData({
							width:'40%'
						})
					}else if(that.data.currentIndex==2){
						that.setData({
							image2:'../../static/deng_yel.png'
						})
						that.setData({
							width:'60%'
						})
					}else if(that.data.currentIndex==3){
						that.setData({
							image3:'../../static/deng_yel.png'
						})
						that.setData({
							width:'80%'
						})
					}else if(that.data.currentIndex==4){
						that.setData({
							image4:'../../static/deng_yel.png'
						})
						that.setData({
							width:'100%'
						})
					}
					
				},230)
            }
		// }
		this.setData({
			xuanZeIndex: this.data.xuanZeIndex + 1
		})
	},
	wenTiClick: function(e) {
		let value = e.currentTarget.dataset.i
		// console.log(value)
		// 1是下一题 0是 上一题
		if (value == 1) {
			// 判断当前是否在最后一题
			if (this.data.currentIndex + 1 == this.data.goodList.length) {
				// 判断当前最后第一题是否选择好答案
				if (this.data.currentIndex == this.data.xuanZeIndex) {
					tt.showToast({
						title: '未选择问题',
						duration: 1500,
						icon: 'none'
					})
				} else {
					// 显示广告
					this.setData({
						guangGao: true
					})
					// 显示广告时 顶部栏背景
					tt.setNavigationBarColor({
						// 字体颜色 仅支持 #ffffff 和 #000000
						frontColor: '#ffffff',
						backgroundColor: '#101742',
					});
					// 总分数
					// console.log(this.score)
				}
			} else {
				// 判断当前页是否是未回答过的
				if (this.data.currentIndex == this.data.xuanZeIndex) {
					tt.showToast({
						title: '未选择问题',
						duration: 1500,
						icon: 'none'
					})
				} else {
					this.setData({
						currentIndex: this.data.currentIndex + 1
					})
				}
			}
		} else {
			if (this.data.currentIndex == 0) {
				tt.showToast({
					duration: 1500,
					icon: "none",
					title: '当前已经是第一道题'
				})
				return
			} else {
				this.setData({
					currentIndex: this.data.currentIndex - 1
				})
			}
		}
	},
	quxiaoBtn: function() {
		this.setData({
			guangGao: false
		})
		tt.setNavigationBarColor({
			// 字体颜色 仅支持 #ffffff 和 #000000
			frontColor: '#ffffff',
			backgroundColor: '#212B82',
		});
	},
	getUserInfo: function() {
        // let is_manage = tt.getStorageSync('is_manage')
		// if (this.data.getAd && is_manage==0) {
		// 	this.setData({
		// 		getAd: false
		// 	})
        //         let share_user_id = tt.getStorageSync('share_user_id')
        //         let userId = tt.getStorageSync('userId')
        //         let data = {"userId":userId,"product_id":this.data.product_id,"app_id":"tt687924844876863140","share_user_id":share_user_id,"palysow":""}
		// 	advertise().then(res => {
        //         console.log(res)
                
		// 		if (res.code == 200) {
		// 			data.palysow = 1
        //             this.ad_record(data)
        //             this.jieguo()
		// 		}
		// 		this.setData({
		// 			getAd: true
		// 		})
		// 	}).catch(err => {
        //         data.palysow = 0
        //         this.ad_record(data)
		// 		if (err.code == 201) {
		// 			tt.showToast({
		// 				title: err.msg,
		// 				icon: 'none'
		// 			})
		// 		} else if (err.code == 203) {

		// 			console.log('err.code', err.code)
		// 		} else {

		// 			tt.showToast({
		// 				title: err.msg,
		// 				icon: 'none'
		// 			})
		// 		}
		// 		this.setData({
		// 			getAd: true
		// 		})
		// 	})
		// }else{
            this.jieguo()
        // }
	},
    ad_record:function (data) {
        let that = this
        tt.request( {
            url: "https://t.doule.shop/index.php/api/dy/douyin/tiku/ks/ad_record?userId="+data.userId+"&product_id="+data.product_id+"&palysow="+data.palysow+"&app_id="+data.app_id+"&share_user_id="+data.share_user_id,
            success: function( res ) {
                
            }
        })
    },
	jieguo: function() {
        console.log(1)
		tt.redirectTo({
			url: '../answer/answer?scoer=' + this.data.score + '&id=' + this.data.goodList[0].product_id,
		})
	},

})
